package com.zxmr.action;

import java.io.IOException;
import java.util.concurrent.ExecutorService;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.QueueingConsumer.Delivery;
import com.zxmr.asyn.AsynRequest;
import com.zxmr.asyn.AsynRequestRegister;
import com.zxmr.rabbit.ChannelFactory;

public class QueueWithoutExchangeReceiver extends QueueReceiver{
	
	public QueueWithoutExchangeReceiver(ExecutorService es,String queue) 
	{	
		super(es,queue);
	}

	@Override
	public void processRequest(Delivery delivery) throws IOException {
		
		String message = new String(delivery.getBody(),"UTF-8");
		
		Response response = JSON.parseObject(message, Response.class);
		String id = response.getRequest().getId();
		AsynRequest asynRequest = AsynRequestRegister.getCallBack(Integer.parseInt(id));
		if(asynRequest != null && asynRequest.markSuccess())
		{
			response.setRequest(asynRequest.getRequest());
			asynRequest.callback(response);
		}

	}
	
	@Override
	public Channel setupChannel() throws IOException {
		Channel channel = ChannelFactory.createChannel();
 
	    channel.queueDeclare(queue, false, false, false, null);
	    return channel;

	}
}
